package tencent;

import java.util.Arrays;

/**
 * @author habitplus
 * @since 2021-08-22 19:47
 */
public class T20210822N1 {

    public ListNode[] solve(int m, ListNode a) {
        // write code here
        ListNode[] ans = new ListNode[m];
        ListNode[] pre = new ListNode[m];
        ListNode nxt;
        int i;
        while (a != null) {
            nxt = a.next;
            a.next = null;
            i = a.val % m;
            if (pre[i] == null) {
                ans[i] = a;
            } else {
                pre[i].next = a;
            }
            pre[i] = a;
            a = nxt;
        }
        return ans;
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(0);
        ListNode node2 = new ListNode(1);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        ListNode node6 = new ListNode(11);
        ListNode node7 = new ListNode(6);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        node6.next = node7;

        ListNode[] ans = new T20210822N1().solve(5, node1);
        System.out.println(Arrays.toString(ans));
    }
}

class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
